<?php
namespace plugin\videoappdownload\model;
use think\Model;
use think\facade\Db;
class PluginVideoappdownloadArticleComments extends Model {
	
	//新增回复
	public function add_comments($obj) {
		//事务 
		Db::startTrans();
		try {
			$uid          =  $obj['uid'];
			//回复文章id
			$article_id  =  $obj['article_id'];
			//判断文章是否存在
			$article_map['id'] = $article_id;
			$article_find =  (new PluginVideoappdownloadArticle())->finds($article_map,[]);
			if (!$article_find) {
				ajaxmsg('内容不存在', 0);
			}
			//父级id 
			$parent_id   = $obj['parent_id'];
			if ($parent_id>0) {
				//判断当前1级回复是否存在
				$parent_map['id'] = $parent_id;
				if (!$this->finds($parent_map)) {
					ajaxmsg('要回复的内容不存在', 0);
				}
			}
			//被回复评论id
			$reply_id     = $obj['reply_id'];
			//回复内容
			$contents     = $obj['contents'];
			//  状态2上架 -1已删除 1审核中 0被拒绝
			$status       = $obj['status'];
			;
			//被回复者uid
			$reply_uid    = $obj['reply_uid'];
			$data=[
									               'uid'           =>   $uid,
									               'article_id'    =>   $article_id,
									               'parent_id'     =>   $parent_id,
									               'reply_uid'     =>   $reply_uid, 
									               'status'        =>   $status,
									               'reply_id'      =>   $reply_id,
									               'create_time'   =>   time(),
									               'create_ip'     =>   request()->ip(), 
									               'contents'      =>   $contents
									            ];
			$get_id   =  $this->adds($data);
			//回复的一级id 当大于0 回复数+1
			if ($parent_id>0) {
				(new PluginVideoappdownloadArticleComments())->where($parent_map)->inc('reply_count')->update();
			}
			//主题回复数 + 1
			(new PluginVideoappdownloadArticle())->where($article_map)->inc('reply')->update();
			//消息提醒 
			Db::commit();
			return $get_id;
		}
		catch(Exception $ex) {
			Db::rollback();
			ajaxmsg('系统错误', 401,$ex);
		}
	}
	//回复点赞
	public function add_like($obj) {
		$uid       =  $obj['uid'];
		$map['id'] =  $obj['comments_id'];
		$find  =  $this->finds($map);
		if (!$find) {
			ajaxmsg('回复不存在', 0 );
		}
		//事务 
		Db::startTrans();
		try {
			$obj['uid']  = $uid;
			$model   =  (new PluginVideoappdownloadArticleCommentsLike())->adds($obj);
			//点赞成功 增加点赞次数
			if ($model) {
				(new PluginVideoappdownloadArticleComments())->where($map)->inc('like_count')->update();
			} else {
				(new PluginVideoappdownloadArticleComments())->where($map)->dec('like_count')->update();
			}
			Db::commit();
			return $model;
		}
		catch(Exception $ex) {
			Db::rollback();
			ajaxmsg('系统错误', 401,$ex);
		}
	}
	//删除回复 
	public function comments_del($obj) {
		$uid         =  $obj['uid'];
		//文章id
		$id          =  $obj['id'];
		//回复id
		$comments_id =  $obj['comments_id'];
		//判断是否是管理员删除 是则添加删除原因 用户本人删除无需 管理员删除通知被当前删除回复的用户
		$field                    =     ["status","delete_time"];
		$map                      =     [];
		$map[]                    =     ["id", "=", $comments_id ];
		
		//当管理时不需要判断是否是当前用户id
		$map[]                    =     ["uid", "=", $uid ];
		
		
		$find  =  $this->finds($map);
		if (!$find) {
			ajaxmsg('回复不存在', 0 );
		}
		//事务 
		Db::startTrans();
		try {
			$obj["status"]        = -1;
			$obj["delete_time"]   = time();
			$model = $this->updates($map, $obj, $field);
			//把回复这条评论的其他都删除
			$parent_id_map                      =     [];
			$parent_id_map[]                    =     ["parent_id", "=", $comments_id ];
			$parent_id_map[]                    =     ["status", "=", 2 ];
			$parent_id_count                    =     $this->counts($parent_id_map);
			if ($parent_id_count>0) {
				$this->updates($parent_id_map, $obj, $field);
			}
			//当这条回复大于0的时候 回复减一
			if ($find['parent_id'] > 0) {
				$map_p                 =     [];
				$map_p[]       =     ["id", "=", $find['parent_id'] ];
				$this->where($map_p)->dec('reply_count')->update();
			}
			//主题回复数 减去已删除的 回复
			$article_map['id']     = $id;
			$article_map['status'] = 2;
			(new PluginVideoappdownloadArticle())->where($article_map)->dec('reply', 1 + $parent_id_count)->update();
			Db::commit();
			return true;
		}
		catch(Exception $ex) {
			Db::rollback();
			ajaxmsg('系统错误', 401,$ex);
		}
	}
	public function paginate($map,$order,$page,$user_id=0) {
	
		//用户表无需关联查询
		$list           =   PluginVideoappdownloadArticleComments::where($map)
						           ->order($order)
						           ->paginate($page)
						           ->each(function($item) use ($user_id) {
			//获取是否点赞
			$Like_map['uid']              =    $user_id;
			$Like_map['comments_id']      =    $item['id'];
			$item['is_like'] = (new PluginVideoappdownloadArticleCommentsLike())->is_exist($Like_map);
			return $item;
		}
		);
		//避免多次查询用户表,去重uid
		$uids=[];
		foreach ($list as $key => $value) {
			array_push($uids,$value['uid']);
			if ($value['reply_uid']>0) {
				array_push($uids,$value['reply_uid']);
			}
		}
		// 统计每个元素的频次
		$uids = array_count_values($uids);
		// 对uid频次进行去重
		$uids = array_keys(array_filter($uids));
		//只查询回复中存在的uid数组 只查询1次
		$user_list  = Db::name('users')->where('uid','in',$uids)->field('uid,face,gender,reg_time,nickname')->select()->toArray();
		$data['list']        =   $list;
		$data['uids']        =   $uids;
		$data['user_list']   =   $user_list;
		return $data;
	}
	public function finds($map) {
		
		$finds = PluginVideoappdownloadArticleComments::where($map)->find();
		return $finds;
	}
	public function updates($map,$request,$field) {
		
		if ($field) {
			foreach ($field as $key => $value) {
				$data[$value] = $request[$value];
			}
		} else {
			$data =$request;
		}
		return PluginVideoappdownloadArticleComments::update($data, $map);
	}
	public function counts($map) {
	
		return PluginVideoappdownloadArticleComments::where($map)->count();
	}
	public function adds($request) {
		
		return    PluginVideoappdownloadArticleComments::insertGetId($request);
	}
	//模型方法
}